package edu.hubu.talentmanagementsystem.controller.impl;

import edu.hubu.talentmanagementsystem.common.CommonResult;
import edu.hubu.talentmanagementsystem.controller.AbsCrudController;
import edu.hubu.talentmanagementsystem.exception.InformationNotFoundException;
import edu.hubu.talentmanagementsystem.exception.InternalServerError;
import edu.hubu.talentmanagementsystem.model.ext.Blog;
import edu.hubu.talentmanagementsystem.service.IBlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author moonlan
 * date 2021/3/2 下午9:50
 */
@SuppressWarnings(value = "unused")
@Api(tags = "blog相关接口")
@RestController
@RequestMapping("/blog")
public class BlogController extends AbsCrudController<Blog, Integer> {

    private final IBlogService service;

    public BlogController(IBlogService service) {this.service = service;}

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataTypeClass = Integer.class)
    @Override
    public CommonResult selectOneById(@PathVariable Integer id, @RequestHeader("Authorization") String token) {
        Blog blog = service.selectById(id)
                .orElseThrow(() -> new InformationNotFoundException("Id=" + id + "的实体未找到"));
        return CommonResult.success(blog);
    }

    @GetMapping("/all")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("查询所有")
    @ApiImplicitParam(name = "order", value = "排序字段，可以为null或空", required = true, dataTypeClass = String.class)
    @Override
    public CommonResult selectAllOrderBy(@RequestHeader(name = "order", required = false)  String order, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectAll(order));
    }

    @GetMapping("/page-all")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("分页查询所有")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "currentPage", value = "当前页，默认为1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，默认为5", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "order", value = "排序字段，可以为null或空", required = true, dataTypeClass = String.class)
    })
    @Override
    public CommonResult selectAllPagedOrderBy(
            @RequestParam(required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize,
            @RequestHeader(name = "order", required = false)  String order, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectAllPaged(currentPage, pageSize, order));
    }

    @PostMapping("/like-page-all")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("分页模糊查询")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "currentPage", value = "当前页，默认为1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，默认为5", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "order", value = "排序字段，可以为null或空", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "blog", value = "企业查询条件", required = true, dataTypeClass = Blog.class)
    })
    @Override
    public CommonResult selectLikePagedOrderBy(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize, @RequestHeader(name = "order", required = false)  String order,
                                               @RequestBody Blog blog, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectLikePaged(currentPage, pageSize, blog, order));
    }

    @PostMapping("/")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("保存")
    @ApiImplicitParam(name = "blog", value = "企业查询条件", required = true, dataTypeClass = Blog.class)
    @Override
    public CommonResult save(@RequestBody Blog blog, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.insert(blog).orElseThrow(() -> new InternalServerError("保存失败")));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("根据ID删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataTypeClass = Integer.class)
    @Override
    public CommonResult deleteById(@PathVariable Integer id, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.deleteById(id));
    }

    @PutMapping("/")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("根据ID修改")
    @ApiImplicitParam(name = "blog", value = "企业修改条件", required = true, dataTypeClass = Blog.class)
    @Override
    public CommonResult updateById(@RequestBody Blog blog, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.updateById(blog.getBlogId(), blog)
                .orElseThrow(() -> new InternalServerError("修改失败")));
    }
}
